<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="elibrary.*" %>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <%

        //the database connection settings are done here

                String host = request.getParameter("host");
                String port = request.getParameter("port");
                String db = request.getParameter("db");
                String dbuser = request.getParameter("dbuser");
                String dbpass = request.getParameter("dbpass");
                String email = request.getParameter("email");
                if(host.equals("") || port.equals("") || db.equals("") || dbuser.equals("") || email.equals("")) {
                        session.setAttribute("message", "Invalid parameters given.");
                        response.sendRedirect("install.jsp");
                }
                String path = getServletContext().getRealPath("/config.jsp");
                String connectURL = "jdbc:db2://"+host+":"+port+"/"+db;
                try{
                        Class.forName("com.ibm.db2.jcc.DB2Driver");
                        Connection conn = DriverManager.getConnection(connectURL, dbuser, dbpass);
		
                        String text = "<%";
                        text = text + "\nString connectURL = "+'"'+connectURL+'"'+";";
                        text = text + "\nString DBuser = "+'"'+dbuser+'"'+";";
                        text = text + "\nString DBpass = "+'"'+dbpass+'"'+";";
                        text = text + "\nString siteEmail = "+'"'+email+'"'+";";
                        text = text + "\n"+"%"+">";
		
                        Config conf = new Config();
                        conf.writeConfig(path,text);
                        response.sendRedirect("index.jsp");
		
                }catch(SQLException e){
                        session.setAttribute("message", "Could not connect to database. Configurations not saved. Check details again");
                        response.sendRedirect("install.jsp");
                }catch(Exception e){
                        session.setAttribute("message", "Could not save the configuration."+e.getMessage());
                        response.sendRedirect("install.jsp");
                }
        %>
    </body>
</html>